package cn.com.wangluotongxin.mall.service.impl;

import cn.com.wangluotongxin.mall.model.MallGoodsStandard;
import cn.com.wangluotongxin.mall.mapper.MallGoodsStandardMapper;
import cn.com.wangluotongxin.mall.rabbitmq.pojo.GoodsInfoPojo;
import cn.com.wangluotongxin.mall.service.IMallGoodsStandardService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 网络通信-商城APP-商品规格 服务实现类
 * </p>
 *
 * @author AutoGenerator
 * @since 2024-11-05
 */
@Service
public class MallGoodsStandardServiceImpl extends ServiceImpl<MallGoodsStandardMapper, MallGoodsStandard> implements IMallGoodsStandardService {

    @Override
    public MallGoodsStandard getByAdminGoodsStandardId(Integer tenantId, Integer mallGoodsId, Integer adminStandardId) {
        return this.getOne(new LambdaQueryWrapper<MallGoodsStandard>()
                .eq(MallGoodsStandard::getTenantId, tenantId)
                .eq(MallGoodsStandard::getMallGoodsId, mallGoodsId)
                .eq(MallGoodsStandard::getAdminStandardId, adminStandardId)
                .last("limit 0,1"));
    }

    @Override
    public MallGoodsStandard goodsStandardSync(Integer mallGoodsId, GoodsInfoPojo pojo) {
        MallGoodsStandard goodsStandard = this.buildGoodsStandard(mallGoodsId, pojo);
        if(goodsStandard.getId() != null && goodsStandard.getId() > 0) {
            this.updateById(goodsStandard);
        } else {
            this.save(goodsStandard);
        }
        return goodsStandard;
    }

    private MallGoodsStandard buildGoodsStandard(Integer mallGoodsId, GoodsInfoPojo pojo) {
        MallGoodsStandard newGoodsStandard = new MallGoodsStandard();

        MallGoodsStandard goodsStandard = this.getByAdminGoodsStandardId(pojo.getTenantId(), mallGoodsId, pojo.getAdminStandardId());
        if(goodsStandard != null) {
            newGoodsStandard.setId(goodsStandard.getId());
        }

        newGoodsStandard.setTenantId(pojo.getTenantId());
        newGoodsStandard.setMallGoodsId(mallGoodsId);
        newGoodsStandard.setAdminStandardId(pojo.getAdminStandardId());
        newGoodsStandard.setStandardId(pojo.getStandardId());
        newGoodsStandard.setStandardName(pojo.getStandardName());
        newGoodsStandard.setColorId(pojo.getColorId());
        newGoodsStandard.setColorName(pojo.getColorName());
        newGoodsStandard.setSizeId(pojo.getSizeId());
        newGoodsStandard.setSizeName(pojo.getSizeName());
        newGoodsStandard.setUnitId(pojo.getUnitId());
        newGoodsStandard.setUnitName(pojo.getUnitName());
        newGoodsStandard.setOriPrice(pojo.getOriPrice());
        newGoodsStandard.setSalePrice(pojo.getSalePrice());
        newGoodsStandard.setStockNumber(pojo.getStockNumber());
        newGoodsStandard.setSaleNumber(pojo.getSaleNumber());
        newGoodsStandard.setStandardImages(pojo.getStandardImages());
        newGoodsStandard.setStandardRemark(pojo.getStandardRemark());
        newGoodsStandard.setIsSale(pojo.getIsSale());
        return newGoodsStandard;
    }
}
